# CFLAGS := -I./ -Wall -Wextra -O2
export CC := arm-linux-gnueabihf-gcc
export SDK_DIR := $(shell pwd)
export SDKCLI_INC := $(SDK_DIR)/sdkcli/inc/
export GDB_FLAG := -g

LDFLAGS := -lm
TARGET := arm_sdk
SRC := $(subst ./,,$(shell find ./ -name *.c))
SUBDIRS := $(shell find . -maxdepth 1 -type d ! -name "." ! -name ".*" ! -name "obj" -printf "%f\n")
SUBOBJS := $(addsuffix /obj, $(SUBDIRS))
OBJ_FILES := $(subst /src/,/obj/,$(SRC:.c=.o))

# Default target
all: $(TARGET)

# 链接所有子目录的 obj 文件
$(TARGET): $(OBJ_FILES)
	@echo "link target files..."
	$(CC) $^ -o $@  
	@echo "generated exec file: $(TARGET) successfully."

$(OBJ_FILES): $(SUBDIRS)
	@for dir in $^; do \
		$(MAKE) -C $$dir; \
	done

# Clean
.PHONY: clean
clean:
	rm -rf $(TARGET)
	@for dir in $(SUBDIRS); do \
		echo "Cleaning $$dir"; \
		$(MAKE) -C $$dir clean; \
	done

# Help
.PHONY: help
help:
	@echo "Usage:"
	@echo "  make     - build project"
	@echo "  make clean - remove build artifacts"

